<template>
  <div class="my-comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id" :title="item.content">
        <template #title>
          <div class="cmt-cell">
            <div class="left-box">
              <van-image fit="cover" round :src="item.aut_photo" />

              <div class="info-box">
                <div class="name">{{ item.aut_name }}</div>
                <div class="content">{{ item.content }}</div>
                <div>
                  <span class="time">{{ $relvTime(item.pubdate) }}</span>
                  <van-button @click="clickReply(item)" plain size="mini"
                    >回复({{ item.reply_count }})</van-button
                  >
                </div>
              </div>
            </div>
            <van-icon name="like-o" />
          </div>
        </template>
      </van-cell>
    </van-list>

    <div class="field-box">
      <van-field
        @keyup.enter="send"
        v-model="cmt"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>

    <!-- bug:点击一次回复之后 后面再点其他回复显示的都是第一次的回复 因为第一次显示的回复一直都在 后面都是对它显示隐藏
       v-if 每次显示都是新建 把之前的销毁 -->
    <van-action-sheet v-model="replyShow" title="发表回复">
      <reply-comment
        v-if="replyShow"
        @add="currentItem.reply_count++"
        :item="currentItem"
      />
    </van-action-sheet>
  </div>
</template>

<script>
import ReplyComment from './ReplyComment.vue'
import { commentAPI, sendAPI } from '@/api/comment'
import { Toast } from 'vant'
export default {
  components: {
    ReplyComment
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      cmt: '',
      offset: null,
      replyShow: false,
      currentItem: {}
    }
  },
  methods: {
    clickReply (item) {
      // 显示出回复面板
      this.replyShow = true
      // 点谁就拿到谁存到currentItem
      // currenItem就会自动流入到子
      this.currentItem = item
    },
    async send () {
      // 非空判断
      if (this.cmt === '') return this.$toast('请输入内容')
      const res = await sendAPI({
        target: this.$route.params.id,
        content: this.cmt
      })
      // console.log(res)
      // 把添加后的评论放到数组里即可
      this.list.unshift(res.data.new_obj)
      // 输入框清空
      this.cmt = ''
    },
    async onLoad () {
      const res = await commentAPI({
        // 因为现在获取的是评论，就要传a
        type: 'a',
        // 获取评论这里传文章id
        source: this.$route.params.id,
        offset: this.offset,
        // 一次查10条
        limit: 10
      })

      // 把本次返回的最后一条存到offset里
      // 作为下一次查询的开始
      this.offset = res.data.last_id
      this.list.push(...res.data.results)
      // 把本次加载状态改为false
      this.loading = false

      // 判断是否全部加载完成
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>
.my-comment {
  padding-bottom: 53px;

  .field-box {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
  ::v-deep .cmt-cell {
    display: flex;
    justify-content: space-between;

    .left-box {
      display: flex;
      .van-image {
        width: 40px;
        height: 40px;
        margin-right: 10px;
      }
      .info-box {
        .name {
          color: #466b9d;
        }
        .content {
          margin: 10px 0;
        }
        .time {
          color: gray;
          margin-right: 10px;
        }
        .van-button {
          vertical-align: middle;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
